//
//  MBConnectingDotView.m
//  MBConnectingDotView
//
//  Created by catch on 16/8/20.
//  Copyright © 2016年 catch. All rights reserved.
//

#import "MBConnectingDotView.h"
#import "UIColor+MB.h"

static const NSUInteger dotNumber = 5;
static const NSTimeInterval durationPerDot = 0.4;

@interface MBConnectingDotView()

@property (nonatomic, assign) BOOL isAnimating;
@property (nonatomic, strong) NSMutableArray<UIView *> *dots;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation MBConnectingDotView

#pragma mark -- Init

-(void)dealloc{
    [_timer invalidate];
    _timer = nil;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup{
    self.dots = [NSMutableArray array];
}

-(void)createDots{
    for (int i = 0; i < dotNumber; i++){
        CGFloat inset = (self.frame.size.width - 40)/(dotNumber-1);
        CGRect frame = CGRectMake(i*(inset+8),.5f*(self.frame.size.height-8),8,8);
        UIView* view = [[UIView alloc]initWithFrame:frame];
        view.backgroundColor = [UIColor colorFromHexString:@"cfecff"];
        view.layer.borderColor = [UIColor colorFromHexString:@"4091c6"].CGColor;
        view.layer.borderWidth = 1.0f;
        view.layer.cornerRadius = 4.0f;
        [self addSubview:view];
        [_dots addObject:view];
    }
}

#pragma mark -- Public Method

-(void)startAnimation{
    if (_isAnimating) {
        return;
    }
    
    if (!_dots || _dots.count < dotNumber) {
        [self createDots];
    }
    
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:durationPerDot*(dotNumber+1) target:self selector:@selector(startDotAnimation) userInfo:nil repeats:YES];
        [_timer fire];
    }
    
    self.isAnimating = YES;
}

-(void)stopAnimation{
    self.isAnimating = NO;
    [_timer invalidate];
    self.timer = nil;
}

-(void)startDotAnimation{
    _dots[0].alpha = 0.0f;
    _dots[1].alpha = 0.0f;
    _dots[2].alpha = 0.0f;
    _dots[3].alpha = 0.0f;
    _dots[4].alpha = 0.0f;
    [UIView animateWithDuration:durationPerDot animations:^{
        _dots[0].alpha = 1.0f;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:durationPerDot animations:^{
            _dots[1].alpha = 1.0f;
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:durationPerDot animations:^{
                _dots[2].alpha = 1.0f;
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:durationPerDot animations:^{
                    _dots[3].alpha = 1.0f;
                } completion:^(BOOL finished) {
                    [UIView animateWithDuration:durationPerDot animations:^{
                        _dots[4].alpha = 1.0f;
                    } completion:^(BOOL finished) {
                        
                    }];
                }];
            }];
        }];
    }];
}

@end